<html>
<head><meta charset="utf-8"><title>const fn/macro to sum all field sizes without padding · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/const.20fn.2Fmacro.20to.20sum.20all.20field.20sizes.20without.20padding.html">const fn/macro to sum all field sizes without padding</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234050510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/const%20fn/macro%20to%20sum%20all%20field%20sizes%20without%20padding/near/234050510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Colin Finck <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/const.20fn.2Fmacro.20to.20sum.20all.20field.20sizes.20without.20padding.html#234050510">(Apr 11 2021 at 14:46)</a>:</h4>
<p>Say I have the following structure and I want to read it with <code>binread</code> (which just reads and deserializes one field after another):</p>
<div class="codehilite"><pre><span></span><code>struct StandardInformationData {
    creation_time: u64,
    last_data_change_time: u64,
    last_mft_change_time: u64,
    last_access_time: u64,
    file_attributes: u32,
}
</code></pre></div>
<p>Is there a const fn/macro to get the sum of all field sizes (here 36), without any padding introduced for alignment?</p>
<p><code>core::mem::size_of</code> would output 40 if the structure fields happen to be 8-byte aligned. I could of course add <code>#[repr(packed(4))]</code>, but it feels wrong to change the in-memory representation of the structure just to get that information (and it can easily be forgotten..)</p>



<a name="234056232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/const%20fn/macro%20to%20sum%20all%20field%20sizes%20without%20padding/near/234056232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/const.20fn.2Fmacro.20to.20sum.20all.20field.20sizes.20without.20padding.html#234056232">(Apr 11 2021 at 15:52)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"></span>
<span class="k">trait</span><span class="w"> </span><span class="n">SumOfFieldSizes</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="k">fn</span> <span class="nf">size</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">usize</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="fm">macro_rules!</span><span class="w"> </span><span class="n">SumOfFieldSizes_</span><span class="w"> </span><span class="p">{(</span><span class="w"></span>
<span class="w">    </span><span class="cp">$(</span><span class="w"> </span><span class="cp">#[$attr:meta]</span><span class="w"> </span><span class="p">)</span><span class="o">*</span><span class="w"></span>
<span class="w">    </span><span class="cp">$pub</span>:<span class="nc">vis</span><span class="w"></span>
<span class="w">    </span><span class="k">struct</span> <span class="cp">$StructName</span>:<span class="nc">ident</span><span class="w"> </span><span class="cp">$(</span><span class="o">&lt;</span><span class="cp">$($lt</span>:<span class="nc">lifetime</span><span class="p">),</span><span class="o">*</span><span class="w"> </span><span class="cp">$(,</span><span class="p">)</span><span class="o">?</span><span class="w"> </span><span class="cp">$($T</span>:<span class="nc">ident</span><span class="p">),</span><span class="o">*</span><span class="w"> </span><span class="cp">$(,</span><span class="p">)</span><span class="o">?&gt;</span><span class="p">)</span><span class="o">?</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="cp">$(</span><span class="w"></span>
<span class="w">        </span><span class="cp">$(</span><span class="w"> </span><span class="cp">#[$field_attr:meta]</span><span class="w"> </span><span class="p">)</span><span class="o">*</span><span class="w"></span>
<span class="w">        </span><span class="cp">$field_pub</span>:<span class="nc">vis</span><span class="w"></span>
<span class="w">        </span><span class="cp">$field_name</span>:<span class="nc">ident</span><span class="w"> </span>: <span class="cp">$FieldTy</span>:<span class="nc">ty</span><span class="w"></span>
<span class="w">      </span><span class="p">),</span><span class="o">*</span><span class="w"> </span><span class="cp">$(,</span><span class="p">)</span><span class="o">?</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="k">impl</span><span class="cp">$(</span><span class="o">&lt;</span><span class="cp">$($lt</span><span class="w"> </span><span class="p">,)</span><span class="o">*</span><span class="w"> </span><span class="cp">$($T</span><span class="w"> </span><span class="p">,)</span><span class="o">*&gt;</span><span class="p">)</span><span class="o">?</span><span class="w"> </span><span class="k">crate</span>::<span class="n">path</span>:<span class="nc">to</span>::<span class="n">SumOfFieldSizes</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="cp">$StructName</span><span class="w"> </span><span class="cp">$(</span><span class="o">&lt;</span><span class="cp">$($lt</span><span class="w"> </span><span class="p">,)</span><span class="o">*</span><span class="w"> </span><span class="cp">$($T</span><span class="w"> </span><span class="p">,)</span><span class="o">*&gt;</span><span class="p">)</span><span class="o">?</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span> <span class="nf">size</span><span class="w"> </span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="mi">0</span><span class="w"> </span><span class="cp">$(</span><span class="o">+</span><span class="w"> </span>::<span class="n">core</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="cp">$FieldTy</span><span class="o">&gt;</span><span class="p">())</span><span class="o">*</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">)}</span><span class="w"></span>
<span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">SumOfFieldSizes_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">SumOfFieldSizes</span><span class="p">;</span><span class="w"></span>

<span class="cp">#[macro_rules_derive(SumOfFieldSizes!)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">StandardInformationData</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">creation_time</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">last_data_change_time</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">last_mft_change_time</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">last_access_time</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">file_attributes</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">assert_eq!</span><span class="p">(</span><span class="n">StandardInformationData</span>::<span class="n">size</span><span class="p">(),</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">4</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="234056419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/const%20fn/macro%20to%20sum%20all%20field%20sizes%20without%20padding/near/234056419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/const.20fn.2Fmacro.20to.20sum.20all.20field.20sizes.20without.20padding.html#234056419">(Apr 11 2021 at 15:54)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4fb498d8a68db4cd1f216a4d76d4e4f3">Playground</a></p>



<a name="234057502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/const%20fn/macro%20to%20sum%20all%20field%20sizes%20without%20padding/near/234057502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/const.20fn.2Fmacro.20to.20sum.20all.20field.20sizes.20without.20padding.html#234057502">(Apr 11 2021 at 16:12)</a>:</h4>
<p><code>const SIZE: usize</code> would be better here, as it is a const</p>



<a name="234064065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/const%20fn/macro%20to%20sum%20all%20field%20sizes%20without%20padding/near/234064065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/const.20fn.2Fmacro.20to.20sum.20all.20field.20sizes.20without.20padding.html#234064065">(Apr 11 2021 at 18:00)</a>:</h4>
<p>I wasn't sure about being able to use <code>size_of::&lt;T&gt;()</code> in an associated constant if generic type parameters were involved, but yes, provided this associated constant is not fed to generic const parameters, all is fine</p>



<a name="234068596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/const%20fn/macro%20to%20sum%20all%20field%20sizes%20without%20padding/near/234068596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/const.20fn.2Fmacro.20to.20sum.20all.20field.20sizes.20without.20padding.html#234068596">(Apr 11 2021 at 19:14)</a>:</h4>
<p><span class="user-mention" data-user-id="232018">@Daniel Henry-Mantilla</span> Yes, they can refer to generic parameters in scope, that's been possible since 1.20.0 (when associated constants were stabilized)<br>
<a href="https://rust.godbolt.org/z/P9YzMcxc4">https://rust.godbolt.org/z/P9YzMcxc4</a><br>
I wrote a macro to emulate generic constants using them.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>